

loadCustom({ selector: '[data-header]', defaultPath: 'header.html', callback: highlightCurrentNav });
// header-loader.js



function highlightCurrentNav() {
    listenScroll();
    const navLinks = document.querySelectorAll('.nav ul li a');
    const currentPage = window.location.pathname.split('/').pop();
    navLinks.forEach(link => {
        const linkPage = new URL(link.href, window.location.origin).pathname.split('/').pop();
        if (linkPage === currentPage) {
            link.parentElement.classList.add('selected');
        } else {
            link.parentElement.classList.remove('selected');
        }
    });
}


function listenScroll() {
    const fl = document.getElementById("fl-logo");
    const fr = document.getElementById("fr-search");
    const placeholder = document.getElementById("fl-placeholder");
    const navContainer = document.getElementById("nav-container");
    const nav = document.querySelector('.nav');

    let isInNav = false;

    window.addEventListener("scroll", () => {
        const scrollTop = window.scrollY || document.documentElement.scrollTop;
        const rect = fl.getBoundingClientRect();
        // 滚动大于等于指定距离，加入悬浮吸顶效果
        if (scrollTop >= 100 && !isInNav) {
            ///1，加入吸附效果
            nav.classList.add('scroll-fixed-nav');
            ///2，把logo，公司名称，加入效果
            fl.classList.add("shrink");
            navContainer.insertBefore(fl, navContainer.firstChild);

            ///3，把搜索框，加入效果
            fr.classList.add("shrink");
            // fr.style.height = '50px';
            // 把 fl 插入到 nav 的最前面
            navContainer.insertBefore(fr, navContainer.firstChild);
            isInNav = true;
        }
        /// 滚动小于指定距离，取消悬浮吸顶效果
        else if (scrollTop <= 50 && isInNav) {
            ///1，移除吸附效果
            nav.classList.remove('scroll-fixed-nav');
            ///2，把logo,公司名称，移除效果
            fl.classList.remove("shrink");
            placeholder.parentNode.insertBefore(fl, placeholder);

            ///3，把搜索框，移除效果
            fr.classList.remove("shrink");
            // fr.style.height = '105px';
            placeholder.parentNode.insertBefore(fr, placeholder);
            isInNav = false;
        }
    });
}



